package com.bj58.groupbuy.action.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import com.bj58.groupbuy.util.IOUtils;


public class ResourcesUtil {

	private static Charset charset;

	public static Charset getCharset() {
		return ResourcesUtil.charset;
	}

	public static InputStream getResourceAsStream(ClassLoader loader,
			String resource) throws IOException {
		InputStream in = null;
		if (loader != null) {
			in = loader.getResourceAsStream(resource);
		}
		if (in == null) {
			in = ClassLoader.getSystemResourceAsStream(resource);
		}
		if (in == null) {
			throw new IOException("Could not find resource " + resource);
		}
		return in;
	}

	public static ClassLoader getClassLoader() {
		return ClassLoader.getSystemClassLoader();
	}

	public static InputStream getResourceAsStream(String resource)throws IOException {
		return ResourcesUtil.getResourceAsStream(getClassLoader(), resource);
	}

	public static Properties getResourceAsProperties(String resource)throws IOException {
		InputStream in = null;
		String propfile = resource;
		in = ResourcesUtil.getResourceAsStream(propfile);
		Properties props = ResourcesUtil.loadProperties(in);
		in.close();
		return props;
	}

	public static Properties loadProperties(InputStream in) throws IOException {
		Reader reader = new InputStreamReader(in, ResourcesUtil.charset);
		BufferedReader buf = new BufferedReader(reader);
		Properties props = new Properties();
		String line = null;
		while ((line = buf.readLine()) != null) {
			int idx = line.indexOf("=");
			if (idx > 0) {
				String key = line.substring(0, idx).trim();
				String value = "";
				if (line.length() > idx) {
					value = line.substring(idx + 1).trim();
				}
				props.put(key, value);
			}
		}
		buf.close();
		reader.close();
		return props;
	}
	
	public static String readString2(InputStream in) {
		BufferedReader buf = null;
		try {
			Reader reader = new InputStreamReader(in, ResourcesUtil.charset);
			buf = new BufferedReader(reader);
			String line = null;
			StringBuilder sb = new StringBuilder();
			while ((line = buf.readLine()) != null) {
				sb.append(line);
			}
			return sb.toString();
		} catch (Exception e) {
			return "";
		}finally{
			IOUtils.closeQuietly(buf);
		}
	}

	public static String readString(String resource) throws IOException{
		InputStream in = getResourceAsStream(resource);
		Reader reader = new InputStreamReader(in, ResourcesUtil.charset);
		BufferedReader buf = new BufferedReader(reader);
		String line = null;
		StringBuilder sb = new StringBuilder();
		while ((line = buf.readLine()) != null) {
			sb.append(line);
		}
		return sb.toString();		
	}
	
	public static List<String> readAsList(InputStream in) {
		BufferedReader buf = null;
		List<String> list = new ArrayList<String>();
		try {
			buf = new BufferedReader(new InputStreamReader(in, ResourcesUtil.charset));
			String line = null;
			while ((line = buf.readLine()) != null) {
				list.add(line);
			}
			return list;
		} catch (Exception e) {
			return null;
		}finally{
			IOUtils.closeQuietly(buf);
		}
	}
	
	public static void setCharset(Charset charset) {
		ResourcesUtil.charset = charset;
	}
}
